package cn.tedu.socket;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 构建一个简易Tomcat服务器
 */
public class Tomcat {
    private ServerSocket serverSocket;
    public Tomcat(int port) throws IOException {
       serverSocket = new ServerSocket(port);
    }
    public void start() throws IOException {
        while (true) {
            System.out.println("等待客户端连接...");
            Socket socket = serverSocket.accept();
            PrintWriter out =
                    new PrintWriter(socket.getOutputStream());
            out.println("HTTP/1.1 200 OK");
            out.println("Content-Type:text/html;charset=UTF-8");
            out.println("");
            out.println("<h1>Hello,World!</h1>");
            out.flush();
        }
    }
    public static void main(String[] args) throws IOException {
           Tomcat tomcat=new Tomcat(8080);
           tomcat.start();
    }
}
